﻿using System.IO.Ports;

namespace Digitalmes.Infrastructure.Common.SerialPorts;

internal sealed class GallerySerialPort : IGallerySerialPort, IDisposable
{
    /// <summary>
    /// SerialPort类串口实例对象,提供串口的读写等操作
    /// </summary>
    private readonly SerialPort _serialPort;

    public GallerySerialPort(SerialPort serialPort)
    {
        _serialPort = serialPort;
    }

    public string? Purpose { get; init; }

    public string PortName => _serialPort.PortName;

    public bool IsOpen => _serialPort.IsOpen;

    public void Open()
    {
        if (!_serialPort.IsOpen)
        {
            _serialPort.Open();
        }
    }

    public void Close()
    {
        if (_serialPort.IsOpen)
        {
            _serialPort.Close();
        }
    }

    public void Dispose()
    {
        _serialPort.Dispose();
    }
}
